package cgl.narada.topicgenerator;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

import cgl.narada.protocol.Protocol;
import cgl.narada.util.ByteUtilities;

public class LongTopicResponse {

   private int clientID;
   private long baseTopic;
   private int numberOfTopics;
   private boolean status; // true means success, false means fail

   public static final boolean FAIL = false;
   public static final boolean SUCCESS = true;

   /**
    * construct a successful response with the given values
    */
   public LongTopicResponse(int clientID, long baseTopic, int numberOfTopics) {
      this.clientID = clientID;
      this.baseTopic = baseTopic;
      this.numberOfTopics = numberOfTopics;
      this.status = true;
   }

   /**
    * construct a fail response for the given clientID
    */
   public LongTopicResponse(int clientID) {
      this.clientID = clientID;
      this.status = false;

      this.baseTopic = 0;
      this.numberOfTopics = 0;
   }

   /**
    * this constructor is used server side to construct a request message
    * from a byte stream received over the network
    * @param data the byte array received over the network
    */
   public LongTopicResponse(byte[] data) {

      clientID = ByteUtilities.getInt(data, 1);

      //set status
      if(data[5] == 1){
         status = true;
      }else{
         status = false;
      }

      baseTopic = ByteUtilities.getLong(data, 6);
      numberOfTopics = ByteUtilities.getInt(data, 14);
   }

   /**
    * @return the clientID of this object
    */
   public int getClientID(){
      return clientID;
   }

   /**
    * @return the baseTopic of this object
    */
   public long getBaseTopic(){
      return baseTopic;
   }

   /**
    * @return the number of topics of this object
    */
   public int getNumberOfTopics(){
      return this.numberOfTopics;
   }

   /**
    * @return the status of this response message
    */
   public boolean isStatusOk(){
      return this.status;
   }

   /**
    * this method is used to serialize an object to send accross the network
    * @return the serialized form of the object
    */
   public byte[] getBytes(){

      int length = 1 + 4 + 1 + 8 + 4;
      byte[] result = new byte[ length ];
      result[0] = Protocol.LONG_TOPIC_EVENT;

      // put the clientID
      byte[] bytes = ByteUtilities.getBytes(clientID);
      int index = 1;
      for (int i = 0; i < bytes.length; i++) {
         result[index++] = bytes[i];
      }

      // put status, 1 for success, 0 for fail
      if(status){
         result[index++] = 1;
      }else{
         result[index++] = 0;
      }

      // put baseTopic
      bytes = ByteUtilities.getBytes(baseTopic);
      for (int i = 0; i < bytes.length; i++) {
         result[index++] = bytes[i];
      }

      // put numberOfTopics
      bytes = ByteUtilities.getBytes(this.numberOfTopics);
      for (int i = 0; i < bytes.length; i++) {
         result[index++] = bytes[i];
      }

      return result;
   }

   public String toString(){
      StringBuffer buffer = new StringBuffer();
      buffer.append("clientID: "+clientID+"\n");
      buffer.append("base topic: "+ this.baseTopic+"\n");
      buffer.append("number of topics: "+this.numberOfTopics+"\n");
      if(status){
         buffer.append("status is OK.\n");
      }else{
         buffer.append("status is FAIL.\n");
      }
      return buffer.toString();
   }

}